import {HTTP} from "../utils/http.js";

export class classicModel extends HTTP {
    getlatest(sCallabck) {
        this.request({
            url: 'classic/latest',
            success: res => {
                sCallabck(res)
                this._setLatestIndex(res.index)
                let key = this._getKey(res.index)
                wx.setStorageSync(key, res);
            }
        })
    }

    getclassic(index, nextOrprevious, sCallabck) {
        let key = nextOrprevious == 'next' ?
            this._getKey(index + 1) : this._getKey(index - 1)
        let classicData = wx.getStorageSync(key);
        if (!classicData) {
            this.request({
                url: `classic/${index}/${nextOrprevious}`,
                success: res => {
                    sCallabck(res)
                    wx.setStorageSync(this._getKey(res.index), res);
                }
            })
        } else {
            sCallabck(classicData)
        }

    }


    isFirst(index) {
        return index == 1 ? true : false
    }

    isLatest(index) {
        let latestIndex = this._getLatestIndex();
        return latestIndex == index ? true : false
    }

    _setLatestIndex(index) {
        wx.setStorageSync('latest', index);
    }

    _getLatestIndex() {
        let index = wx.getStorageSync('latest');
        return index
    }

    _getKey(index) {
        let key = `classic-${index}`
        return key
    }

}